/**
 * 加载职业/职位.
 * @param pid
 */
function loadPosition(pid,positionId,positionLevelId,config){
	var positionId= positionId || "positionId";
	var positionLevelId= positionLevelId || "positionLevelId";
	config = config || {};
	var dataJson = {pid:pid,apiUrl:"system/getPositions"};
	ajaxForSelect(dataJson,function(rs){
		var datas = rs.datas;
		var html = '<option value="-1">请选择</option>';
		if(pid == 0){
			for(var i=0;i<datas.length;i++){
				var d = datas[i];
				html += '<option value="'+d.id+'">'+d.name+'</option>';
			}
			$("#"+positionId).html(html);
			if(config.positionIdValue){
				$('#'+positionId).change(function(){
					loadPosition(this.value,positionId,positionLevelId,config);
				});
				$('#'+positionId).val(config.positionIdValue).trigger('change');
			}else{
				$('#'+positionId).change(function(){
					loadPosition(this.value,positionId,positionLevelId);
				});
			}
		}else{
			for(var i=0;i<datas.length;i++){
				var d = datas[i];
				html += '<option value="'+d.id+'">'+d.name+'</option>';
			}
			$("#"+positionLevelId).html(html);
			
			if(config.positionLevelIdValue){
				$('#'+positionLevelId).val(config.positionLevelIdValue);
			}
			
		}
	},{isShow:false,async:(typeof isAsync!=="undefined"?false:true)});
}


function loadPositionOnClick(pid,positionId,positionLevelId){
	if(pid == ""){
		loadPosition(pid);
	}
}


function loadScript(jsPath,loadMethod){
	$.getScript(jsPath,function(){
		eval(loadMethod+';');
	});
}


/**
 * 加载短信国际区号.
 */
function loadSMSCountryCode(config){
	var _countryAndRegion = "_countryAndRegion2018";
	
	if($("#"+_countryAndRegion).length>0){
		$("#"+_countryAndRegion).toggle();
		return;
	}
	
	config= config || {};
	config.browser = config.browser || 'h5';
	config.container = config.container || 'body';
	config.showId = config.showId || "smsCountryCode";
	
	var dataJson = {apiUrl:"common/getDatasNoLogin",action:"smsCountryCode"};
	ajaxForSelect(dataJson,function(rs){
		if(config.browser == 'h5'){
			var html=''; 
			var datas = rs.datas;
			var size = datas.length;
			
			html += '﻿    <div id="'+_countryAndRegion+'" class="countryAndRegion">'; 
			html += '<div class="countryAndRegionSearchContainer">'
			html += '        <h1>选择国家和地区</h1>';
			html += '        <input type="text" onkeyup="smsCountryCodeInputBlur(this.value,\''+_countryAndRegion+'\')" name="" class="countryAndRegionSearch" placeholder="输入国家或者区号" style="background:url(\'res/img/search@2x.png\') no-repeat 20px center;background-size: auto 60%;">';
			html += '</div>'
			html += '        <ul class="withdrawal_status">'; 
			for(var i=0;i<size;i++){
				var d= datas[i];
				html += '            <li onclick="smsCountryCodeLiClick(\'+'+d.code+'\',\''+config.showId+'\',\''+_countryAndRegion+'\')" data-value="'+d.nameCn+":"+d.nameEn+":"+d.code+":"+d.pinyin+'">'; 
				html += '                <span>'+d.nameCn+'</span>'; 
				html += '                <span>+'+d.code+'</span>'; 
				html += '            </li>';
			}
			html += '        </ul>'; 
			html += '    </div>'; 
			$(config.container).append(html);
			$("#"+_countryAndRegion).toggle();
		}
	},{});
}

function smsCountryCodeLiClick(code,showId,_countryAndRegion){
	//兼容表单和容器元素.
	$("#"+showId).val(code);
	$("#"+showId).html(code);
	
	$("#"+_countryAndRegion).toggle();	
}

function smsCountryCodeInputBlur(value,_countryAndRegion){
	$("#"+_countryAndRegion).find("ul li").show();
	if(value == ''){
		return;
	}
	$("#"+_countryAndRegion).find("ul li").each(function(){
		var tmpValue = $(this).attr("data-value");
		if(tmpValue.indexOf(value) == -1){
			$(this).hide();
		}else{
			$(this).show();
		}
	});
}


function loadArea(pid,provinceId,cityId,config){
	var provinceId = provinceId || "provinceId";
	var cityId = cityId || "cityId";
	config= config || {};
	var dataJson = {parentId:pid,apiUrl:"common/area/getArea"};
	ajaxForSelect(dataJson,function(rs){
		var datas = rs.datas;
		var html = '<option value="-1">请选择</option>';
		if(pid == 0){
			for(var i=0;i<datas.length;i++){
				var d = datas[i];
				html += '<option value="'+d.aid+'">'+d.name+'</option>';
			}
			$("#"+provinceId).html(html);
			if(config.provinceIdValue){
				$('#'+provinceId).change(function(){
					loadArea(this.value,provinceId,cityId,config);
				});
				$('#'+provinceId).val(config.provinceIdValue).trigger('change');
			}else{
				$('#'+provinceId).change(function(){
					loadArea(this.value,provinceId,cityId);
				});
			}
		}else{
			for(var i=0;i<datas.length;i++){
				var d = datas[i];
				html += '<option value="'+d.aid+'">'+d.name+'</option>';
			}
			$("#"+cityId).html(html);
			if(config.cityIdValue){
				//$('#'+cityId).val(config.cityId).trigger('change');
				$('#'+cityId).val(config.cityIdValue);
			}
		}
	},{isShow:false});
}

function loadDealer(delarId){
	var delarId = delarId || "delarId";
	var dataJson = {apiUrl:"dealer/getAllDealerList"};
	ajaxForSelect(dataJson,function(rs){
		var datas = rs.datas;
		var html = '<option value="-1">请选择</option>';
		html += '<option value="0">平台</option>';
		for(var i=0;i<datas.length;i++){
			var d = datas[i];
			html += '<option value="'+d.dealerUid+'">'+d.dealerName+'</option>';
		}
		$("#"+delarId).html(html);
	},{isShow:false});
}


function loadAreaOnClick(pid){
	if(pid == ""){
		loadArea(pid);
	}
}

/**
 * 通过手机号获取用户信息;
 * @param mobile
 * @param fn
 */
function getUserByMobile(mobile,fn){
	var dataJson = {mobile:mobile,apiUrl:"userInfo/getUserByMobile"};
	ajaxForSelect(dataJson,function(rs){
		fn.call(this,rs);
	},{isShow:false});
}

function reloadCompany(uid){
	if(uid != 0){
		var dataJson = {apiUrl:"company/getCompanyById",companyUid:uid,reloadUser:true};
		ajaxForSelect(dataJson,function(rs){
			var d = rs.datas;
			if(d){
				if(d.auditState == -1){
					 $("#auditImg").hide();
					$("#auditReson").html("待完善资料：两天试用期");
				}else if(d.auditState == 0){
					$("#auditImg").attr("src","https://cdn.c8hr.com/api_files/res/img/common/rzing@2x.png");
					$("#auditReson").html("审核中：两天试用期");
				}else if(d.auditState == 1){
					$("#auditImg").attr("src","https://cdn.c8hr.com/api_files/res/img/common/rzok@2x.png");
				}else if(d.auditState == 2){
					$("#auditImg").attr("src","https://cdn.c8hr.com/api_files/res/img/common/rzfalse@2x.png");
					$("#auditImg").addClass("renzheng2");
					$("#auditReson").html("审核未通过："+d.auditReason);
				}
				
				if(d.isVIP == 1){
					$("#vipSpan").html("<a class='vip'>VIP用户</a><a class='dqtime'>"+new Date(d.expireTime).format("yyyy.MM.dd")+"到期</a><a href='home/vip' class='btn btn-link mt-30'>续费</a>");
				}
				reloadPunCompany(puid);
		 }else{
			 $("#auditImg").hide();
			 $("#auditReson").html("待完善资料");
			 setTimeout(function(){
				 alert("您的企业资料还未进行完善，请先完善资料");
				 gotoPage("home/company/ucenter_edit");
			 },1000);
		 }
		},{isShow:false});
	}
}

function reloadPunCompany(puid){
	if(puid != 0){
		var dataJson = {apiUrl:"company/getCompanyById",companyUid:puid,reloadUser:true};
		ajaxForSelect(dataJson,function(rs){
			var d = rs.datas;
			if(d){
				$("#puNames").html("主账号企业名称："+d.companyName);
			}
		},{isShow:false});
	}
}
